package com.alvin.abp.service;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Service;

import com.alvin.abp.domain.Rule;

@Service(value="ruleService")
public class RuleService {

	private NamedParameterJdbcTemplate jdbcTemplate;

	@Autowired
	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
	}
	
	@SuppressWarnings("unchecked")
	public Rule readRuleById(Long id) {
		String sql = "SELECT `ID`, `ACTION`, `POINT` FROM T_RULE WHERE `ID`=?";
		try {
			Rule rule = (Rule) jdbcTemplate.getJdbcOperations()
					.queryForObject(sql, new Object[] { id },
							new BeanPropertyRowMapper(Rule.class));
			return rule;
		} catch (EmptyResultDataAccessException ex) {
			return null;
		}

	}
}
